package com.txtsqlclient.domain;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class DataSourceFactory {

	private static DataSourceFactory factory;

	public final static List<DataSourceType> LEGAL_DATABASE_TYPES;

	public final static List<DataSourceType> LEGAL_FILE_DATASOURCE_TYPES;

	static {
		LEGAL_DATABASE_TYPES = Collections.unmodifiableList(Arrays.asList(new DataSourceType[] { DataSourceType.MYSQL,
				DataSourceType.POSTGRESQL, DataSourceType.SQL_SERVER,
				DataSourceType.SYBASE }));
		LEGAL_FILE_DATASOURCE_TYPES = Collections.unmodifiableList(Arrays
				.asList(new DataSourceType[] { DataSourceType.CSV }));
	}
	/**
	 * Protected constructor to force singleton.
	 */
	protected DataSourceFactory() {
	}

	public static DataSourceFactory getInstance() {
		if(factory == null) {
			factory = new DataSourceFactory();
		}
		return factory;
	}

	public DataSource getDataSource(final DataSourceType dsType) {
		DataSource ds = null;
		if (LEGAL_DATABASE_TYPES.contains(dsType)) {
			ds = new Database(dsType);
		} else if (LEGAL_FILE_DATASOURCE_TYPES.contains(dsType)) {
			ds = new FileDataSource(dsType);
		} else {
			throw new IllegalArgumentException("Given Data Source Type, :"
					+ dsType
					+ " is neither a valid Database nor a File Data Source.");
		}
		return ds;
	}
}